/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.servlets;

import com.jmooc.entity.Answer;
import com.jmooc.entity.Course;
import com.jmooc.entity.CourseModule;
import com.jmooc.entity.Question;
import com.jmooc.service.AnswerService;
import com.jmooc.service.CourseModuleService;
import com.jmooc.service.CourseService;
import com.jmooc.service.QuestionService;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Group
 */
@WebServlet(name = "InitServlet", urlPatterns = {"/init"})
public class InitServlet extends HttpServlet {

    @EJB
    private CourseService courseService;
    
    @EJB
    private QuestionService questionService;
    
    @EJB
    private AnswerService answerService;
    
    @EJB
    private CourseModuleService courseModuleService;

    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        initMicrosoft();
        initAndroid();
    }
    
    void initMicrosoft()
    {
        Course sampleCourse = new Course();
        sampleCourse.setCertificationName("Windows Certified Master Lvl1");
        sampleCourse.setName("Windows certification course");
        sampleCourse.setDuration(10L);
        courseService.create(sampleCourse);

        CourseModule sampleModule1 = new CourseModule();
        sampleModule1.setContent("<h3>Le menu démarrer</h3><p>Il permet notamment, contrairement à ce que son nom indique, d'éteindre l'ordinateur.</p><iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/Q9ORVhokjkk\" frameborder=\"0\" allowfullscreen></iframe>");
        sampleModule1.setCourse(sampleCourse);
        sampleModule1.setTitle("Démarrons !");
        courseModuleService.create(sampleModule1);

        CourseModule sampleModule2 = new CourseModule();
        sampleModule2.setContent("<h3>Le cultissime Microsoft Paint</h3><p>Il permet la retouche graphique, selon Microsoft</p><p>Il est facile de réaliser ce genre de choses après 5 ou 10 minutes</p><iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/v2g5qbvb7F4\" frameborder=\"0\" allowfullscreen></iframe>");
        sampleModule2.setCourse(sampleCourse);
        sampleModule2.setTitle("MSPAINT !");
        courseModuleService.create(sampleModule2);

        Question question1 = new Question();
        question1.setCourse(sampleCourse);
        question1.setTitle("Is Windows Open Source ?");
        questionService.create(question1);

        Answer oui = new Answer();
        oui.setQuestion(question1);
        oui.setTitle("Yes");
        answerService.create(oui);

        Answer non = new Answer();
        non.setQuestion(question1);
        non.setTitle("No");
        answerService.create(non);

        question1.setGoodAnswer(non);
        questionService.update(question1);

        Question question2 = new Question();
        question2.setCourse(sampleCourse);
        question2.setTitle("Which corporations sells Windows ?");
        questionService.create(question2);

        Answer google = new Answer();
        google.setQuestion(question2);
        google.setTitle("Google");
        answerService.create(google);

        Answer microsoft = new Answer();
        microsoft.setQuestion(question2);
        microsoft.setTitle("Microsoft");
        answerService.create(microsoft);

        Answer apple = new Answer();
        apple.setQuestion(question2);
        apple.setTitle("Apple");
        answerService.create(apple);

        question2.setGoodAnswer(microsoft);
        questionService.update(question2);
        
        Question question3 = new Question();
        question3.setCourse(sampleCourse);
        question3.setTitle("What's MS Paint ?");
        questionService.create(question3);

        Answer toshop = new Answer();
        toshop.setQuestion(question3);
        toshop.setTitle("Photoshop");
        answerService.create(toshop);

        Answer gimp = new Answer();
        gimp.setQuestion(question3);
        gimp.setTitle("Gimp");
        answerService.create(gimp);

        Answer santa = new Answer();
        santa.setQuestion(question3);
        santa.setTitle("Santa Claus drawing program");
        answerService.create(santa);
        
        Answer dev = new Answer();
        dev.setQuestion(question3);
        dev.setTitle("A development language");
        answerService.create(dev);

        question3.setGoodAnswer(santa);
        questionService.update(question3);
    }
    
    void initAndroid()
    {
        Course sampleCourse = new Course();
        sampleCourse.setCertificationName("Android lvl1");
        sampleCourse.setName("Initiation à Android");
        sampleCourse.setDuration(3L);
        courseService.create(sampleCourse);

        CourseModule sampleModule1 = new CourseModule();
        sampleModule1.setContent("<h3>Qu'est ce qu'Android ?</p>");
        sampleModule1.setCourse(sampleCourse);
        sampleModule1.setTitle("Android ?! WTF ?!");
        courseModuleService.create(sampleModule1);

        CourseModule sampleModule2 = new CourseModule();
        sampleModule2.setContent("<h3>Comment allumer son Android</h3><p>Comme un Windows Phone</p>");
        sampleModule2.setCourse(sampleCourse);
        sampleModule2.setTitle("Faut l'allumer");
        courseModuleService.create(sampleModule2);

        Question question1 = new Question();
        question1.setCourse(sampleCourse);
        question1.setTitle("Est ce qu'Android est beau ?");
        questionService.create(question1);

        Answer oui = new Answer();
        oui.setQuestion(question1);
        oui.setTitle("Oui");
        answerService.create(oui);

        Answer non = new Answer();
        non.setQuestion(question1);
        non.setTitle("Non");
        answerService.create(non);

        question1.setGoodAnswer(oui);
        questionService.update(question1);

        Question question2 = new Question();
        question2.setCourse(sampleCourse);
        question2.setTitle("Combien coûte Android");
        questionService.create(question2);

        Answer beaucoup = new Answer();
        beaucoup.setQuestion(question2);
        beaucoup.setTitle("Beaucoup");
        answerService.create(beaucoup);

        Answer pasassez = new Answer();
        pasassez.setQuestion(question2);
        pasassez.setTitle("Pas assez");
        answerService.create(pasassez);

        Answer gratuit = new Answer();
        gratuit.setQuestion(question2);
        gratuit.setTitle("Bah c'est gratuit, mais les constructeurs donnent de l'argent à Microsoft.");
        answerService.create(gratuit);

        question2.setGoodAnswer(gratuit);
        questionService.update(question2);
    }
  
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
